﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;

namespace ud_gSDMS.Cat
{
    public partial class DanhMucLoaiSP : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        protected string _pageId;

        void Page_Load(object sender, EventArgs e)
        {
            if (SiteUtils.GetCurrentSiteUser()==null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                _pageId = pageId.ToString();

                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            LoadGrid();
        }
        private void LoadGrid()
        {
            var data = gSDMS_BL.ud_gSDMS_CAT_PRODUCT_PACKAGE_Search(null, null, txtName.Text,  chkActive.Checked);

            if (ViewState["SortExpression"]!=null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }

            grdProduct.DataSource = data;
            grdProduct.DataBind();
        }

        private void grdHostPackage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdProduct.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdHostPackage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gSDMS_BL.ud_gSDMS_CAT_PRODUCT_PACKAGE_Del(e.CommandArgument.ToString(), false);
            }
        }
        void grdHostPackage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdHostPackage_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            LoadGrid();
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdProduct.PageIndexChanging += grdHostPackage_PageIndexChanging;
            grdProduct.RowCommand += grdHostPackage_RowCommand;
            grdProduct.RowDeleting += grdHostPackage_RowDeleting;
            grdProduct.Sorting += grdHostPackage_Sorting;

            btnSearch.Click += btnSearch_Click;
            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }


        void btnFinish_Click(object sender, EventArgs e)
        {
            WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}